<%@page import="com.wh.minihelp.user.domain.ChildInfo"%>
<%@page import="com.wh.minihelp.user.util.UserUtil"%>
<%@page import="com.wh.minihelp.user.domain.FamilyAccount"%>
<%@page import="com.wh.minihelp.flower.domain.FamilyFlower"%>
<%@page import="java.util.List"%>
<%@ page language="java" pageEncoding="UTF-8"%>

<div class="column_title">
	<span>已添加的小孩列表</span>
	<div class="column_title_oper">
		<a href="#" onclick="expand()">收起/展开</a>
	</div>
</div>
<div id="div_child" class="weui-cells weui-cells-ex">
	<%
		List<ChildInfo> childs = (List<ChildInfo>) request.getAttribute("childs");
		size = childs.size();
		for (int i = 0; i < size; i++) {
			ChildInfo ff = childs.get(i);
	%>
	<cc data="<%=ff.getId()%>">
	<div class="weui-cell weui-cell-ex">
		<c><%=(i + 1)%></c>
		<d>.<%=ff.getName()%></d>
	</div>
	<div class="weui-cell-oper">
		<a href="#" onclick="delChild()">删除</a> &nbsp; <a href="#" onclick="changeChildName()">修改</a>
	</div>
	</cc>
	<%
		}
	%>
	<div class="weui-cells-oper">
		<a id="btnAddChild" href="#" class="weui-btn weui-btn_mini weui-btn_primary">添加新小孩</a>
	</div>
</div>
<div style='display: none'>
	<div style='display: none' id="div_addChild_none">
		<div id='div_addChild'>
			<label class="weui-label"><B>小孩名称:</B></label> <input class="weui-input" type="text" id="addChildName" placeholder="请输入小孩名称">
		</div>
	</div>
	<cc id="cc_child" data="">
	<div class="weui-cell weui-cell-ex">
		<c></c>
		<d></d>
	</div>
	<div class="weui-cell-oper">
		<a href="#" onclick="delChild()" class="ui-link">删除</a> &nbsp; <a href="#" onclick="changeChildName()" class="ui-link">修改</a>
	</div>
	</cc>
</div>
<div style='padding: 5px;'></div>

<script type="text/javascript">
	<%if (isManager) {%>
$(function(){
	$('#btnAddChild').on('click', function(){
		$("#js_dialog_title").text("添加新的小孩"); 
		$("#js_dialog_content").append($("#div_addChild"));
		okFunction=addChild;
		cancelFunction=cancelChild;
		$androidDialog1.css('display','')
	});
});
	function validChild(){
		if ($.trim($("#addChildName").val()).length == 0) {
			error("#addChildName","请输入小孩名称！")
			return false;
		}
		if(!checkInput($("#addChildName").val())){
			error("#addChildName","小孩名称中有特殊字符，请重新输入！")
			return false;
		}
		return true;
	}
	function cancelChild(){
		$("#div_addChild_none").append($("#div_addChild"));
		$androidDialog1.css('display','none')
	}
	function addChild(){		
		if(!validChild())return;
		$.post("<%=path%>/u?m=AC"
		  , { name: $("#addChildName").val()}	   
		  ,function(data){
				if(data.success){
					cancelChild();
					var last=$("#div_child cc :last");
					var newLi=null;
					if(last.length==0){
						newLi=$("#cc_child").clone();
					}else{
						newLi=last.clone();
					}
					newLi.attr('data',data.value.id);
					newLi.find("d").text("."+data.value.name);
					if(last.length==0){
						newLi.insertBefore($("#div_child .weui-cells-oper"));
					}else{
						newLi.insertAfter(last);
					}
					resortCC("div_child");
				}else{
					alert(data.msg)
				}
		   }, "json");		
	}
	function changeChildName(){
		var cur=$(event.srcElement).parents("cc");
		var data=cur.attr("data");
		$("#js_dialog_title").text("修改小孩名称"); 
		$("#js_dialog_content").append($("#div_addChild"));
		// $("#div_addChild input").attr('placeholder','ddddddddddd') 设置小孩名称 
		$("#div_addChild label").attr('data',data)// 记录id到label上去
		okFunction=updChild;
		cancelFunction=cancelChild;
		$androidDialog1.css('display','')
	}
	function updChild(){			
		if(!validChild())return;
		var id=$("#div_addChild label").attr('data');
		$.post("<%=path%>/u?m=UC"
		  , {id:id, name: $("#addChildName").val()}	   
		  ,function(data){
				if(data.success){
					cancelChild();
					$("#div_child cc[data="+id+"] d").text("."+$("#addChildName").val())
				}else{
					alert(data.msg)
				}
		   }, "json");		
	}
	function delChild(){
		var cur=$(event.srcElement).parents("cc");
		$.post("<%=path%>/u?m=DC", {
			id : cur.attr("data")
		}, function(data) {
			if (data.success) {
				cur.remove();
				resortCC("div_child");
			} else {
				alert(data.msg)
			}
		}, "json");
	}
<%}%>
	
</script>